﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.Text;
using System.Data;

namespace IbnBazSQL
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            
            string [] str = getInstalledInstance();
            string[] str1 = getInstalledInstance32();
            List<string> lstAll = new List<string>();
            lstAll.AddRange(str);
            lstAll.AddRange(str1);

            FileInfo fiCurrent = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);

            string dir = fiCurrent.DirectoryName;
            if (!dir.EndsWith("\\"))
                dir += "\\";

            StringBuilder sw = new StringBuilder();

            foreach (string strIn in lstAll)
            {
                sw.AppendLine (strIn);
            }


            File.WriteAllText(dir + "SqlIns.txt", sw.ToString());


            
        }       

        

        private static string [] getInstalledInstance()
        {

            





            RegistryKey rk = Registry.LocalMachine;
            RegistryKey sqlServer = null;
            try { sqlServer = rk.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server"); }
            catch { }
            if (sqlServer == null)
            {
                
                return new string[0];
            }

            object keyValue = null;

            try { keyValue = sqlServer.GetValue("InstalledInstances"); }
            catch { }

            if (keyValue == null)
            {
                return new string[0];
            }

            string[] strInstances = keyValue as string[];

            if (strInstances == null)
            {
                return new string[0];
            }

            if (strInstances.Length == 0)
            {
                return new string[0];
            }


            for (int i = 0; i < strInstances.Length; i++)
            {


                if (strInstances[i] == "MSSQLSERVER")
                {
                    strInstances[i] = ".";
                }
                else
                {
                    strInstances[i] = ".\\" + strInstances[i];

                }
            }

            return strInstances;







        }



        private static string[] getInstalledInstance32()
        {




            RegistryKey rk = Registry.LocalMachine;

            RegistryKey rksoft = null;

            try { rksoft = rk.OpenSubKey("SOFTWARE\\Wow6432Node"); }
            catch { }
            if (rksoft == null)
            {

                return new string[0];
            }


            
            RegistryKey sqlServer = null;
            try { sqlServer = rksoft.OpenSubKey("Microsoft\\Microsoft SQL Server"); }
            catch { }
            if (sqlServer == null)
            {

                return new string[0];
            }

            object keyValue = null;

            try { keyValue = sqlServer.GetValue("InstalledInstances"); }
            catch { }

            if (keyValue == null)
            {
                return new string[0];
            }

            string[] strInstances = keyValue as string[];

            if (strInstances == null)
            {
                return new string[0];
            }

            if (strInstances.Length == 0)
            {
                return new string[0];
            }


            for (int i = 0; i < strInstances.Length; i++)
            {


                if (strInstances[i] == "MSSQLSERVER")
                {
                    strInstances[i] = ".";
                }
                else
                {
                    strInstances[i] = ".\\" + strInstances[i];

                }
            }

            return strInstances;







        }
    }
}
